package com.cmict.data.system.api.permission;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.util.json.JsonUtils;
import com.cmict.data.system.api.permission.dto.RoleDTO;
import com.cmict.data.system.convert.permission.RoleConvert;
import com.cmict.data.system.dal.dataobject.permission.RoleDO;
import com.cmict.data.system.enums.ApiConstants;
import com.cmict.data.system.service.permission.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = ApiConstants.VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class RoleApiImpl implements RoleApi {

    @Resource
    private RoleService roleService;

    @Override
    public CommonResult<Boolean> validRoles(Collection<Long> ids) {
        roleService.validRoles(ids);
        return success(true);
    }

    @Override
    public CommonResult<List<RoleDTO>> getRolesByIdsJoin(String ids) {
        List<RoleDO> roles = roleService.getRoleList(Arrays.stream(ids.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        return success(RoleConvert.INSTANCE.convertList04(roles));
    }
    @Override
    public CommonResult<List<RoleDTO>> getRoles(Collection<Long> ids) {
        List<RoleDO> roles = roleService.getRoleList(ids);
        List<RoleDTO> roleDTOS = RoleConvert.INSTANCE.convertList04(roles);
        return success(roleDTOS);
    }
}
